﻿using System.Threading;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using VoiceShieldCS;

namespace BasicNumberTalk
{
    public class Program
    {
        const byte Point_Word = 64; //Sound slot of the word point
        const byte Volts_Word = 62; //Sound slot of the word volts
        const byte Hundred_Word = 28; //Sound slot of the word Hundred
        const byte Thousand_Word = 29; //Sound slot of the word Thousand
        const float refVolts = 3.3f; //Volts on hi side

        static VoiceShield vs = new VoiceShield(80);

        public static void Main()
        {
            var analogPin = new AnalogInput(Pins.GPIO_PIN_A1); //Analog pin we will say value of

            int pinVal = 0;
            int oldPinVal = 0;

            while (true)
            {
                pinVal = analogPin.Read();
                if (pinVal != oldPinVal)
                {
                    oldPinVal = pinVal;

                    SayNumber(pinVal);
                    Thread.Sleep(1000);
                    SayVolts(pinVal);
                }
                Thread.Sleep(5000);
            }
        }

        private static void SayVolts(int number)
        {
            int bigNumber = 0;
            int smallNumber = 0;

            float volt = ((float)number / 1023) * refVolts;
            bigNumber = (int)System.Math.Floor(volt);
            smallNumber = (int)System.Math.Floor(System.Math.Round((volt * 10) - (bigNumber * 10)));

            if (bigNumber != 0)
            {
                vs.ISDPlayToEOM((byte)bigNumber);
            }
            vs.ISDPlayToEOM(Point_Word);
            vs.ISDPlayToEOM((byte)smallNumber);
            vs.ISDPlayToEOM(Volts_Word); 
        }

        private static void SayNumber(int number)
        {
            int thous = 0;
            int hunds = 0;
            int tens = 0;
            int ones = 0;

            thous = System.Math.Abs(number / 1000);
            hunds = System.Math.Abs((number - (thous * 1000)) / 100);
            tens = System.Math.Abs((number - (thous * 1000) - (hunds * 100)) / 10);
            ones = System.Math.Abs((number - (thous * 1000) - (hunds * 100) - (tens * 10)));

            if (thous != 0)
            {
                vs.ISDPlayToEOM((byte)thous);
                vs.ISDPlayToEOM(Thousand_Word);
            }

            if (hunds != 0)
            {
                vs.ISDPlayToEOM((byte)hunds);
                vs.ISDPlayToEOM(Hundred_Word);
            }

            if (tens != 0)
            {
                switch (tens)
                {
                    case 1:
                        vs.ISDPlayToEOM((byte)(10 + ones)); //10 to 19
                        break;
                    case 2:
                        vs.ISDPlayToEOM(20);        //Twenty
                        SayOnes(ones);
                        break;
                    case 3:
                        vs.ISDPlayToEOM(21);        //Thirty
                        SayOnes(ones);
                        break;
                    case 4:
                        vs.ISDPlayToEOM(22);        //Fourty
                        SayOnes(ones);
                        break;
                    case 5:
                        vs.ISDPlayToEOM(23);        //Fifty
                        SayOnes(ones);
                        break;
                    case 6:
                        vs.ISDPlayToEOM(24);        //Sixty
                        SayOnes(ones);
                        break;
                    case 7:
                        vs.ISDPlayToEOM(25);        //Seventy
                        SayOnes(ones);
                        break;
                    case 8:
                        vs.ISDPlayToEOM(26);        //Eighty
                        SayOnes(ones);
                        break;
                    case 9:
                        vs.ISDPlayToEOM(27);        //Ninety
                        SayOnes(ones);
                        break;
                }
            }
            else if (ones != 0)
            {
                vs.ISDPlayToEOM((byte)ones);
            }
        }

        private static void SayOnes(int ones)
        {
            if (ones != 0)
            {
                vs.ISDPlayToEOM((byte)ones);
            }
        }
    }
}
